//游戏相关函数的实现
#include"game.h"



//初始化
void InitBoard(char board[ROW][COL], int row, int col)
{
    int i=0;
    int j=0;
    for ( i = 0; i < row; i++)
    {
        for ( j = 0; j < col; j++)
        {
            board[i][j]=' ';
        }
        
    }
    
}


//显示棋盘
void DisplayBoard(char board[ROW][COL],int row, int col)
{
    int i=0;
    for (i=0; i<row; i++)
    {
        int j=0;
        for (j=0;j<col;j++)
        {
            if (j==col-1)
            {
                printf(" %c \n",board[i][j]);
                break;
            }
            else
            {
                printf(" %c |",board[i][j]);
            }
        }
        //(@ _ @)
        if(i<row-1)
        {
            int m=0;
            for (m=0;m<row ;m++)
            {
                if (m<row-1)
                {
                    printf("---|");
                }
                else
                {
                    printf("---\n");
                }
            }
        }
    }
}

//玩家回合
void Player_Move(char board[][COL],int row,int col)
{
    int x=0;
    int y=0;
    printf("Player Turn\n");
    while(1)
    {
        printf("Input Your Coordinate:>");
        scanf("%d",&x);
        if (x<=9)
        {
            scanf("%d",&y);
        }
        else
        {
            y =x % 10;
            x =x / 10;
        }
        

        //判断坐标合法性
        if(x>0 && x<=row && y>0 && y<=col)
        {
            //玩家输入和系统内置数据的转换
            x=x-1;
            y=y-1;

            //判断坐标是否占用
            if(board[x][y]==' ')
            {
                board[x][y]='*';
                break;
            }
            else
            {
                Red;
                printf("Coordinate Is Occupied,Input Agint!!!\n");
                Color;
            }
        }
        else
        {
            Red;
            printf("Erro,There Is Not The Coordinate,Please Agint!!!\n");
            Color;
        }
    }
    //输入一个数后,打印一次棋盘
    DisplayBoard(board,ROW,COL);
}

//电脑回合
void Computer_Move( char board[ROW][COL],int row,int col,int t)
{
    printf("Computer Turn\n");


    int x=0;
    int y=0;
    while(1)
    {
        //随机数生成
        srand(t);
        x =rand() %(row) ;
        y =rand() %(col) ;

        //判断坐标是否占用
        if(board[x][y]==' ')
        {
            board[x][y]='#';
            break;//break是对while作用的
        }
        t++;
    }

    DisplayBoard(board,ROW,COL);
}



//判断输赢
//返回值  1  0   2   3
//输赢   赢  输  平  续
int Win(char x)
{
    //玩家赢
    if (x =='*')
    {
        return 1;
    }
    //电脑赢
    else
    {
        return 0;
    }
}

int Return_IsWin(char board[ROW][COL],int row,int col)
{
    int i =0;

    //判断win
    //行
    for (i=0;i<row;i++)
    {
        if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0]!=' ')
        {
            return Win(board[i][0]);
        }
    }
    //列
    for ( i = 0; i < col; i++)
    {
        if(board[0][i]==board[1][i] && board[1][i]==board[2][i] && board[0][i]!=' ')
        {
            return (board[0][i]);
        }
    }
    //对角
    if (board[0][0]==board[1][1] && board[1][1]== board[2][2] && board[0][0] !=' ')
    {
        return (board[1][1]);
    }
    if (board[2][0]==board[1][1] && board[1][1]== board[0][2] && board[0][2] !=' ')
    {
            return Win(board[1][1]);
    }


    //判断平局
    for (i=0;i<row;i++)
    {
        int j=0;
        for(j=0;j<col;j++)
        {
            if (board[i][j] ==' ')
            {
                return 3;//遇到棋盘空格(棋盘未满)返回3
            }
        }
        
    }
    return 2;//全部满了,返回2
    
}

//判断返回值
void IsWin(int ret,int lucky)
{
    Un_Red;
    if(ret == 1)
    {
        printf("You Win!!!");
        lucky++;
    }
    else if (ret ==0)
    {
        printf("You Lose!!!");
    }
    else if(ret ==2)
    {
        printf("DRAW");
    }
    Color;
    printf("\n");
    system("pause");//暂停一下
    if (lucky>3)
    {
        system("start https://autopatchcn.yuanshen.com/client_app/download/launcher/20240513153024_R4Y2Siji8AadjI0Q/pcweb/yuanshen_setup_202405121033.exe");
    }
    
}